/*
 *
 * Copyright 2021-2024 NXP.
 *
 * NXP Confidential. This software is owned or controlled by NXP and may only be
 * used strictly in accordance with the applicable license terms. By expressly
 * accepting such terms or by downloading,installing, activating and/or otherwise
 * using the software, you are agreeing that you have read,and that you agree to
 * comply with and are bound by, such license terms. If you do not agree to be
 * bound by the applicable license terms, then you may not retain, install, activate
 * or otherwise use the software.
 *
 */
#include "UwbApi_Utility.h"
#include "uci_ext_defs.h"
#include "phOsalUwb.h"
#include "phNxpLogApis_UwbApi.h"

#if UWBFTR_TWR
static void parseTwoWayRangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData);
#endif // UWBFTR_TWR

#if UWBFTR_DL_TDoA_Tag
static void parseDlTDoARangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData);
#endif // UWBFTR_DL_TDoA_Tag

#if UWBFTR_UL_TDoA_Anchor
static void parseOneWayRangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData);
#endif // UWBFTR_UL_TDoA_Anchor

#if (UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)
static void parseOwrWithAoaNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData);
#endif // (UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)

#if UWBFTR_Radar
static void parseRadarCirNtf(uint8_t *p, uint16_t len, phUwbRadarNtf_t *pRadarNtf);
static void parseRadarTestNtf(uint8_t *p, uint16_t len, phUwbRadarNtf_t *pRadarNtf);
#endif // UWBFTR_Radar

tUWBAPI_STATUS sendUciCommandAndWait(uint16_t event, uint16_t cmdLen, uint8_t *pCmd)
{
    tUWBAPI_STATUS status;

    status = UWA_SendUciCommand(event, cmdLen, pCmd, 0);

    if (UWBAPI_STATUS_OK == status) {
        if (phOsalUwb_ConsumeSemaphore_WithTimeout(uwbContext.devMgmtSem, UWB_MAX_DEV_MGMT_RSP_TIMEOUT) !=
            UWBSTATUS_SUCCESS) {
            LOG_E("%s : event timedout", __FUNCTION__);
            return UWBAPI_STATUS_TIMEOUT;
        }
        status = uwbContext.wstatus;
    }
    return status;
}

tUWBAPI_STATUS sendUciCommand(uint16_t event, uint16_t cmdLen, uint8_t *pCmd, uint8_t pbf)
{
    tUWBAPI_STATUS status;

    status = UWA_SendUciCommand(event, cmdLen, pCmd, pbf);
    if (UWBAPI_STATUS_OK == status) {
        status = uwbContext.wstatus;
    }
    return status;
}

uint16_t serializeSessionInitPayload(uint32_t sessionHandle, eSessionType sessionType, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
    offset = (uint16_t)(offset + sizeof(sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, sessionType);
    offset = (uint16_t)(offset + sizeof(uint8_t));
    return offset;
}

uint16_t serializeGetCoreConfigPayload(uint8_t noOfParams, uint8_t paramLen, uint8_t *paramId, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    pCmdBuf[0]      = noOfParams;
    offset          = (uint16_t)(offset + sizeof(noOfParams) + paramLen);
    if (noOfParams > 0) {
        phOsalUwb_MemCopy(&pCmdBuf[1], paramId, paramLen);
    }
    return offset;
}

uint16_t serializeSessionHandlePayload(uint32_t sessionHandle, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
    offset = (uint16_t)(offset + sizeof(sessionHandle));
    return offset;
}

uint16_t serializeAppConfigPayload(uint32_t sessionHandle, uint8_t noOfParams, uint16_t paramLen, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
    offset = (uint16_t)(offset + sizeof(sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, noOfParams);
    offset = (uint16_t)(offset + sizeof(noOfParams) + paramLen);

    return offset;
}

uint16_t serializeUpdateControllerMulticastListPayload(
    phMulticastControleeListContext_t *pControleeContext, uint8_t *pCmdBuf)
{
    uint16_t offset              = 0;
    uint16_t subSessionKeyOffset = 0;
    UWB_UINT32_TO_STREAM(pCmdBuf, pControleeContext->sessionHandle);
    offset = (uint16_t)(offset + sizeof(pControleeContext->sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, pControleeContext->action);
    offset = (uint16_t)(offset + sizeof(pControleeContext->action));

    UWB_UINT8_TO_STREAM(pCmdBuf, pControleeContext->no_of_controlees);
    offset = (uint16_t)(offset + sizeof(pControleeContext->no_of_controlees));

    if (pControleeContext->no_of_controlees > 0) {
        for (uint8_t i = 0; i < pControleeContext->no_of_controlees; i++) {
            UWB_UINT16_TO_STREAM(pCmdBuf, pControleeContext->controlee_list[i].short_address);
            UWB_UINT32_TO_STREAM(pCmdBuf, pControleeContext->controlee_list[i].subsession_id);

            if (pControleeContext->action == KUWB_Add16BSubSessionKey) {
                UWB_ARRAY_TO_STREAM(
                    pCmdBuf, pControleeContext->controlee_list[i].subsession_key, SUB_SESSION_KEY_LEN_16B);
                subSessionKeyOffset += SUB_SESSION_KEY_LEN_16B;
            }
            else if (pControleeContext->action == KUWB_Add32BSubSessionKey) {
                UWB_ARRAY_TO_STREAM(
                    pCmdBuf, pControleeContext->controlee_list[i].subsession_key, SUB_SESSION_KEY_LEN_32B);
                subSessionKeyOffset += SUB_SESSION_KEY_LEN_32B;
            }
        }
        offset = (uint16_t)(offset + pControleeContext->no_of_controlees * SHORT_ADDRESS_LEN +
                            pControleeContext->no_of_controlees * SUBSESSION_HANDLE_LEN + subSessionKeyOffset);
    }

    return offset;
}

uint16_t serializeTestDataPayload(uint16_t psduLen, uint8_t psduData[], uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    if (psduLen > 0) {
        phOsalUwb_MemCopy(pCmdBuf, psduData, psduLen);
        offset = (uint16_t)(offset + psduLen);
    }
    return offset;
}
#if !(UWBIOT_UWBD_SR040)
uint16_t serializedoVcoPllCalibPayload(uint8_t channel, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));
    return offset;
}

uint16_t serializeSetCalibPayload(
    uint8_t channel, eCalibParam paramId, uint8_t *calibrationValue, uint16_t length, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));

    if (((paramId >> 8) & 0xFF) != EXTENTED_CALIB_PARAM_ID) {
        UWB_UINT8_TO_STREAM(pCmdBuf, paramId);
        offset = (uint16_t)(offset + sizeof(uint8_t));

        UWB_UINT8_TO_STREAM(pCmdBuf, length);
        offset = (uint16_t)(offset + sizeof(uint8_t));
    }
#if UWBIOT_UWBD_SR150
    else {
        UWB_UINT8_TO_STREAM(pCmdBuf, (paramId >> 8));
        UWB_UINT8_TO_STREAM(pCmdBuf, (paramId & 0x00FF));
        offset = (uint16_t)(offset + sizeof(uint16_t));

        UWB_UINT16_TO_STREAM(pCmdBuf, length);
        offset = (uint16_t)(offset + sizeof(uint16_t));
    }
#endif // UWBIOT_UWBD_SR150

    UWB_ARRAY_TO_STREAM(pCmdBuf, calibrationValue, length);
    offset = (uint16_t)(offset + length);

    return offset;
}

uint16_t serializeGetCalibPayload(uint8_t channel, eCalibParam paramId, uint8_t rxAntenaPair, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));

    UWB_UINT8_TO_STREAM(pCmdBuf, paramId);
    offset = (uint16_t)(offset + sizeof(uint8_t));

#if UWBFTR_AoA_FoV
    if (paramId == AOA_ANTENNAS_PDOA_CALIB) {
        UWB_UINT8_TO_STREAM(pCmdBuf, rxAntenaPair);
        offset = (uint16_t)(offset + sizeof(uint8_t));
    }
    else
#endif // UWBFTR_AoA_FoV
    {
        PHUWB_UNUSED(rxAntenaPair);
    }

    return offset;
}

/*******************************************************************************
**
** Function         serializeGetExtCalibPayload
**
** Description      serialize Get Ext Calib Payload
**
** Returns          Length of payload
**
*******************************************************************************/
uint16_t serializeGetExtCalibPayload(uint8_t channel, eCalibParam paramId, uint8_t rxAntenaPair, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    uint16_t paramSubId;
    /**
     * 1:  1 Byte channel Id
     * 2:  2 Bytes Param Id (Ext Id E0 + 00 subParamId)
     * 3:  1 Byte rxAntenaPair Based on the Use case
     */
    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));

    UWB_UINT8_TO_STREAM(pCmdBuf, (paramId >> 8));
    paramSubId = (paramId & 0x00FF);
    UWB_UINT8_TO_STREAM(pCmdBuf, (paramId & 0x00FF));
    offset = (uint16_t)(offset + sizeof(uint16_t));
    switch (paramSubId) {
#if UWBFTR_AoA_FoV
    case UCI_EXT_PARAM_ID_AOA_ANTENNAS_PDOA_CALIB: {
        UWB_UINT8_TO_STREAM(pCmdBuf, rxAntenaPair);
        offset = (uint16_t)(offset + sizeof(uint8_t));
    } break;
#endif // UWBFTR_AoA_FoV
    default: {
        PHUWB_UNUSED(rxAntenaPair);
    } break;
    }

    return offset;
}
#if (UWBFTR_SE_SN110)
uint16_t serializeSeLoopTestPayload(uint16_t loopCnt, uint16_t timeInterval, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT16_TO_STREAM(pCmdBuf, loopCnt);
    offset = (uint16_t)(offset + sizeof(loopCnt));

    UWB_UINT16_TO_STREAM(pCmdBuf, timeInterval);
    offset = (uint16_t)(offset + sizeof(timeInterval));

    return offset;
}
#endif //(UWBFTR_SE_SN110)
#if (UWBIOT_UWBD_SR100T)
uint16_t serializecalibIntegrityProtectionPayload(eCalibTagOption tagOption, uint16_t calibBitMask, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, tagOption);
    offset = (uint16_t)(offset + sizeof(uint8_t));

    UWB_UINT16_TO_STREAM(pCmdBuf, calibBitMask);
    offset = (uint16_t)(offset + sizeof(calibBitMask));
    return offset;
}

uint16_t serializeVerifyCalibDataPayload(uint8_t *pCmacTag, uint8_t tagOption, uint16_t tagVersion, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_ARRAY_TO_STREAM(pCmdBuf, pCmacTag, UCI_TAG_CMAC_LENGTH);
    offset = (uint16_t)(offset + UCI_TAG_CMAC_LENGTH);

    UWB_UINT8_TO_STREAM(pCmdBuf, tagOption);
    offset = (uint16_t)(offset + sizeof(tagOption));

    UWB_UINT16_TO_STREAM(pCmdBuf, tagVersion);
    offset = (uint16_t)(offset + sizeof(tagVersion));
    return offset;
}

uint16_t serializeConfigureAuthTagOptionsPayload(
    uint8_t deviceTag, uint8_t modelTag, uint16_t labelValue, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, deviceTag);
    offset = (uint16_t)(offset + sizeof(deviceTag));

    UWB_UINT8_TO_STREAM(pCmdBuf, modelTag);
    offset = (uint16_t)(offset + sizeof(modelTag));

    UWB_UINT16_TO_STREAM(pCmdBuf, labelValue);
    offset = (uint16_t)(offset + sizeof(labelValue));

    return offset;
}

uint16_t serializeConfigureAuthTagVersionPayload(uint16_t labelValue, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT16_TO_STREAM(pCmdBuf, labelValue);
    offset = (uint16_t)(offset + sizeof(labelValue));

    return offset;
}
#endif // UWBIOT_UWBD_SR100T
#if (UWBFTR_SE_SN110)
uint16_t serializeUrskDeletionRequestPayload(uint8_t noOfSessionHandle, uint32_t *pSessionHandleList, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT8_TO_STREAM(pCmdBuf, noOfSessionHandle);
    offset = (uint16_t)(offset + sizeof(noOfSessionHandle));

    for (int i = 0; i < noOfSessionHandle; i++) {
        UWB_UINT32_TO_STREAM(pCmdBuf, pSessionHandleList[i]);
        offset = (uint16_t)(offset + sizeof(uint32_t));
    }

    return offset;
}
#endif //(UWBFTR_SE_SN110)

uint16_t serializeWriteOtpCalibDataPayload(
    uint8_t channel, uint8_t writeOption, uint8_t writeDataLen, uint8_t *writeData, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));

    UWB_UINT8_TO_STREAM(pCmdBuf, writeOption);
    offset = (uint16_t)(offset + sizeof(writeOption));

    UWB_UINT8_TO_STREAM(pCmdBuf, writeDataLen);
    offset = (uint16_t)(offset + sizeof(writeDataLen));

    UWB_ARRAY_TO_STREAM(pCmdBuf, writeData, writeDataLen);
    offset = (uint16_t)(offset + writeDataLen);

    return offset;
}

uint16_t serializeReadOtpCalibDataPayload(
    uint8_t channel, uint8_t readOption, eOtpCalibParam calibParam, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT8_TO_STREAM(pCmdBuf, channel);
    offset = (uint16_t)(offset + sizeof(channel));

    UWB_UINT8_TO_STREAM(pCmdBuf, readOption);
    offset = (uint16_t)(offset + sizeof(readOption));

    UWB_UINT8_TO_STREAM(pCmdBuf, calibParam);
    offset = (uint16_t)(offset + sizeof(uint8_t));

    return offset;
}

#if !(UWBIOT_UWBD_SR040)
uint16_t serializeControllerHusSessionPayload(phControllerHusSessionConfig_t *pHusSessionCfg, uint8_t *pCmdBuf)
{
    uint16_t offset              = 0;
    uint8_t update_time_bytesize = 8;

    UWB_UINT32_TO_STREAM(pCmdBuf, pHusSessionCfg->sessionHandle);
    offset = (uint16_t)(offset + sizeof(pHusSessionCfg->sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_count);
    offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_count));

    UWB_ARRAY_TO_STREAM(pCmdBuf, pHusSessionCfg->update_time, update_time_bytesize);
    offset = (uint16_t)(offset + update_time_bytesize);

    for (int i = 0; i < pHusSessionCfg->phase_count; i++) {
        UWB_UINT32_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].phase_sessionHandle);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].phase_sessionHandle));

        UWB_UINT16_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].start_slot_index);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].start_slot_index));

        UWB_UINT16_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].end_slot_index);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].end_slot_index));

        UWB_UINT8_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].phase_participation);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].phase_participation));

        UWB_ARRAY_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].mac_addr, MAC_SHORT_ADD_LEN);
        offset = (uint16_t)(offset + MAC_SHORT_ADD_LEN);
    }

    return offset;
}

uint16_t serializeControleeHusSessionPayload(phControleeHusSessionConfig_t *pHusSessionCfg, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT32_TO_STREAM(pCmdBuf, pHusSessionCfg->sessionHandle);
    offset = (uint16_t)(offset + sizeof(pHusSessionCfg->sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_count);
    offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_count));

    for (int i = 0; i < pHusSessionCfg->phase_count; i++) {
        UWB_UINT32_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].phase_sessionHandle);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].phase_sessionHandle));

        UWB_UINT8_TO_STREAM(pCmdBuf, pHusSessionCfg->phase_list[i].phase_participation);
        offset = (uint16_t)(offset + sizeof(pHusSessionCfg->phase_list[i].phase_participation));
    }

    return offset;
}

uint16_t serializeDtpcmPayload(phDataTxPhaseConfig_t *phDataTxPhaseCfg, uint8_t *pCmdBuf)
{
    uint16_t offset        = 0;
    uint16_t ranging_slots = 0;

    UWB_UINT32_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpcm_SessionHandle);
    offset = (uint16_t)(offset + sizeof(phDataTxPhaseCfg->dtpcm_SessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpcm_Repetition);
    offset = (uint16_t)(offset + sizeof(phDataTxPhaseCfg->dtpcm_Repetition));

    UWB_UINT8_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dataTransferCtrl);
    offset = (uint16_t)(offset + sizeof(phDataTxPhaseCfg->dataTransferCtrl));

    UWB_UINT8_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpml_size);
    offset = (uint16_t)(offset + sizeof(phDataTxPhaseCfg->dtpml_size));

    ranging_slots = GET_RANGING_SLOTS(phDataTxPhaseCfg->dataTransferCtrl);

    for (int i = 0; i < phDataTxPhaseCfg->dtpml_size; i++) {
        if (phDataTxPhaseCfg->dataTransferCtrl & 0x01) {
            UWB_ARRAY_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpml[i].mac_addr, MAC_EXT_ADD_LEN);
            offset = (uint16_t)(offset + MAC_EXT_ADD_LEN);
        }
        else {
            UWB_ARRAY_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpml[i].mac_addr, MAC_SHORT_ADD_LEN);
            offset = (uint16_t)(offset + MAC_SHORT_ADD_LEN);
        }
        UWB_ARRAY_TO_STREAM(pCmdBuf, phDataTxPhaseCfg->dtpml[i].slot_bitmap, ranging_slots);
        offset = (uint16_t)(offset + ranging_slots);
    }

    return offset;
}
#endif //!(UWBIOT_UWBD_SR040)

#if (UWBFTR_DL_TDoA_Anchor || UWBFTR_DL_TDoA_Tag)
uint16_t serializeUpdateActiveRoundsAnchorPayload(uint32_t sessionHandle,
    uint8_t nActiveRounds,
    UWB_MacAddressMode_t macAddressingMode,
    const phActiveRoundsConfig_t roundConfigList[],
    uint8_t *pCmdBuf)
{
    uint16_t offset           = 0;
    uint16_t dst_mac_add_len  = 0;
    uint16_t responderSlotLen = 0;

    /** adding parameter session ID */
    UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
    offset = (uint16_t)(offset + sizeof(sessionHandle));

    /** adding parameter nActiveRounds */
    UWB_UINT8_TO_STREAM(pCmdBuf, nActiveRounds);
    offset = (uint16_t)(offset + sizeof(nActiveRounds));

    for (int i = 0; i < nActiveRounds; i++) {
        /** adding parameter nActiveRounds */
        UWB_UINT8_TO_STREAM(pCmdBuf, roundConfigList[i].roundIndex);
        offset = (uint16_t)(offset + sizeof(roundConfigList[i].roundIndex));
        /** adding parameter rangingRole */
        UWB_UINT8_TO_STREAM(pCmdBuf, roundConfigList[i].rangingRole);
        offset = (uint16_t)(offset + sizeof(roundConfigList[i].rangingRole));
        /** if ranging role is Initiator adding Subsequent fields */
        if (roundConfigList[i].rangingRole == 1) {
            /** adding parameter noofResponders */
            UWB_UINT8_TO_STREAM(pCmdBuf, roundConfigList[i].noofResponders);
            offset = (uint16_t)(offset + sizeof(roundConfigList[i].noofResponders));
            /** depending upon the macAddressingMode adding  responderMacAddressList*/
            if (macAddressingMode == kUWB_MacAddressMode_2bytes) {
                dst_mac_add_len = MAC_SHORT_ADD_LEN * roundConfigList[i].noofResponders;
            }
            else if (macAddressingMode == kUWB_MacAddressMode_8bytes) {
                dst_mac_add_len = MAC_EXT_ADD_LEN * roundConfigList[i].noofResponders;
            }
            else {
                offset = 0;
                NXPLOG_UWBAPI_E("%s: %d: Invalid MacAddressingMode ", __FUNCTION__, __LINE__);
                break;
            }
            UWB_ARRAY_TO_STREAM(pCmdBuf, roundConfigList[i].responderMacAddressList, dst_mac_add_len);
            offset = (uint16_t)(offset + dst_mac_add_len);
            /** adding parameter responderSlotScheduling
             *  depending upon responderSlotScheduling *responderSlots will be added
             */
            if (roundConfigList[i].responderSlotScheduling == 0) {
                UWB_UINT8_TO_STREAM(pCmdBuf, roundConfigList[i].responderSlotScheduling);
                offset = (uint16_t)(offset + sizeof(uint8_t));
            }
            else if ((roundConfigList[i].responderSlots == NULL) ||
                     (roundConfigList[i].responderMacAddressList == NULL)) {
                offset = 0;
                break;
            }
            else {
                responderSlotLen = roundConfigList[i].noofResponders;
                UWB_ARRAY_TO_STREAM(pCmdBuf, roundConfigList[i].responderSlots, responderSlotLen);
                offset = (uint16_t)(offset + responderSlotLen);
            }
        }
    }

    return offset;
}

uint16_t serializeUpdateActiveRoundsReceiverPayload(
    uint32_t sessionHandle, uint8_t nActiveRounds, const uint8_t roundIndexList[], uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
    offset = (uint16_t)(offset + sizeof(sessionHandle));

    UWB_UINT8_TO_STREAM(pCmdBuf, nActiveRounds);
    offset = (uint16_t)(offset + sizeof(nActiveRounds));

    for (int i = 0; i < nActiveRounds; i++) {
        UWB_UINT8_TO_STREAM(pCmdBuf, roundIndexList[i]);
        offset = (uint16_t)(offset + sizeof(roundIndexList[i]));
    }

    return offset;
}
#endif // UWBFTR_DL_TDoA_Anchor || UWBFTR_DL_TDoA_Tag
#endif //!(UWBIOT_UWBD_SR040)

uint16_t serializeTrngtPayload(uint8_t trng_size, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT8_TO_STREAM(pCmdBuf, trng_size);
    offset = (uint16_t)(offset + sizeof(trng_size));
    return offset;
}

#if UWBIOT_UWBD_SR040
uint16_t serializeSessionNvmPayload(esessionNvmManage sesNvmManageTag, uint32_t sessionHandle, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    switch (sesNvmManageTag) {
    case SESSION_NVM_MANAGE_PERSIST:
#if 0
    case SESSION_NVM_MANAGE_DELETE: /* Not available */
#endif

        UWB_UINT8_TO_STREAM(pCmdBuf, sesNvmManageTag);
        offset = (uint16_t)(offset + sizeof(uint8_t));
        UWB_UINT32_TO_STREAM(pCmdBuf, sessionHandle);
        offset = (uint16_t)(offset + sizeof(sessionHandle));
        break;
    case SESSION_NVM_MANAGE_DELETE_ALL:
        UWB_UINT8_TO_STREAM(pCmdBuf, sesNvmManageTag);
        offset = (uint16_t)(offset + sizeof(uint8_t));
        break;
    default:
        return offset;
    }

    return offset;
}

#endif

#if (UWBIOT_UWBD_SR040 || UWBIOT_UWBD_SR150 || UWBIOT_UWBD_SR100S || UWBIOT_UWBD_SR250)
uint16_t serializeSetProfileParamsPayload(
    phUwbProfileInfo_t *pProfileInfo, uint16_t blobSize, uint8_t *pProfileBlob, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT8_TO_STREAM(pCmdBuf, pProfileInfo->profileId);
    offset = (uint16_t)(offset + sizeof(pProfileInfo->profileId));
    UWB_UINT8_TO_STREAM(pCmdBuf, pProfileInfo->deviceType);
    offset = (uint16_t)(offset + sizeof(pProfileInfo->deviceType));
    UWB_ARRAY_TO_STREAM(pCmdBuf, pProfileInfo->mac_addr, MAC_SHORT_ADD_LEN);
    offset = (uint16_t)(offset + MAC_SHORT_ADD_LEN);
    UWB_UINT8_TO_STREAM(pCmdBuf, pProfileInfo->deviceRole);
    offset = (uint16_t)(offset + sizeof(pProfileInfo->deviceRole));
    UWB_ARRAY_TO_STREAM(pCmdBuf, pProfileBlob, blobSize);
    offset = (uint16_t)(offset + blobSize);
    return offset;
}

uint16_t serializeUwbDeviceConfigData(UwbDeviceConfigData_t *pUwbDeviceConfig, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->spec_ver_major, sizeof(pUwbDeviceConfig->spec_ver_major));
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->spec_ver_major));
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->spec_ver_minor, sizeof(pUwbDeviceConfig->spec_ver_minor));
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->spec_ver_minor));
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->chip_id, sizeof(pUwbDeviceConfig->chip_id));
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->chip_id));
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->chip_fw_version, sizeof(pUwbDeviceConfig->chip_fw_version));
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->chip_fw_version));
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->mw_version, sizeof(pUwbDeviceConfig->mw_version));
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->mw_version));
    UWB_UINT32_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->supported_profile_ids);
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->supported_profile_ids));
    UWB_UINT8_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->ranging_role);
    offset = (uint16_t)(offset + sizeof(pUwbDeviceConfig->ranging_role));
    UWB_ARRAY_TO_BE_STREAM(pCmdBuf, pUwbDeviceConfig->device_mac_addr, MAC_SHORT_ADD_LEN);
    offset = (uint16_t)(offset + MAC_SHORT_ADD_LEN);
    return offset;
}
void serializeUwbPhoneConfigData(UwbPhoneConfigData_t *pUwbPhoneConfig, uint8_t *pCmdBuf)
{
    UWB_BE_STREAM_TO_ARRAY(pCmdBuf, &pUwbPhoneConfig->spec_ver_major[0], MAX_SPEC_VER_LEN);
    UWB_BE_STREAM_TO_ARRAY(pCmdBuf, &pUwbPhoneConfig->spec_ver_minor[0], MAX_SPEC_VER_LEN);
    UWB_BE_STREAM_TO_UINT32(pUwbPhoneConfig->session_id, pCmdBuf);
    UWB_STREAM_TO_UINT8(pUwbPhoneConfig->preamble_id, pCmdBuf);
    UWB_STREAM_TO_UINT8(pUwbPhoneConfig->channel_number, pCmdBuf);
    UWB_STREAM_TO_UINT8(pUwbPhoneConfig->profile_id, pCmdBuf);
    UWB_STREAM_TO_UINT8(pUwbPhoneConfig->device_ranging_role, pCmdBuf);
    UWB_BE_STREAM_TO_ARRAY(pCmdBuf, &pUwbPhoneConfig->phone_mac_address[0], SHORT_ADDRESS_LEN);
}
#endif // (UWBIOT_UWBD_SR040 || UWBIOT_UWBD_SR150 || UWBIOT_UWBD_SR100S || UWBIOT_UWBD_SR250)

#if UWBFTR_DataTransfer
uint16_t serializeSendDataPayload(phUwbDataPkt_t *pSendData, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;

    UWB_UINT32_TO_STREAM(pCmdBuf, pSendData->sessionHandle);
    offset = (uint16_t)(offset + sizeof(pSendData->sessionHandle));

    UWB_ARRAY_TO_STREAM(pCmdBuf, pSendData->mac_address, MAC_EXT_ADD_LEN);
    offset = (uint16_t)(offset + MAC_EXT_ADD_LEN);

    UWB_UINT16_TO_STREAM(pCmdBuf, pSendData->sequence_number);
    offset = (uint16_t)(offset + sizeof(pSendData->sequence_number));

    UWB_UINT16_TO_STREAM(pCmdBuf, pSendData->data_size);
    offset = (uint16_t)(offset + sizeof(pSendData->data_size));

    if (pSendData->data_size > 0) {
        phOsalUwb_MemCopy(pCmdBuf, pSendData->data, pSendData->data_size);
        offset = (uint16_t)(offset + pSendData->data_size);
    }

    return offset;
}

#endif // UWBFTR_DataTransfer

#if UWBFTR_TWR // support only for DSTWR
/**
 *
 * Function:        parseTwoWayRangingNtf
 *
 * Description:     Extracts Ranging Params from the given byte array for two way ranging
 *
 * Returns:         None
 *
 */
static void parseTwoWayRangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData)
{
    uint8_t i = 0;
    if (pRngData->no_of_measurements > MAX_NUM_RESPONDERS) {
        NXPLOG_UWBAPI_E("%s: Wrong number of measurements received:%d", __FUNCTION__, pRngData->no_of_measurements);
        return;
    }
    for (i = 0; i < pRngData->no_of_measurements; i++) {
        if (pRngData->mac_addr_mode_indicator == SHORT_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_twr[i].mac_addr[0], p, MAC_SHORT_ADD_LEN);
        }
        else if (pRngData->mac_addr_mode_indicator == EXTENDED_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_twr[i].mac_addr[0], p, MAC_EXT_ADD_LEN);
        }
        else {
            NXPLOG_UWBAPI_E("%s: Invalid mac addressing indicator", __FUNCTION__);
            return;
        }
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].status, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].nLos, p);
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_twr[i].distance, p);
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_twr[i].aoa_azimuth, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].aoa_azimuth_FOM, p);
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_twr[i].aoa_elevation, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].aoa_elevation_FOM, p);
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_twr[i].aoa_dest_azimuth, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].aoa_dest_azimuth_FOM, p);
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_twr[i].aoa_dest_elevation, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].aoa_dest_elevation_FOM, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].slot_index, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_twr[i].rssi, p);
        /** Skip the RFU bytes
         * if mac address format is short, then skip 11 bytes
         * if mac address format is extended, then skip 5 bytes */
        if (pRngData->mac_addr_mode_indicator == SHORT_MAC_ADDRESS) {
            p = p + RFU_SHORT_MAC_ADD;
        }
        else {
            p = p + RFU_EXT_MAC_ADD;
        }
        /* Update the received notification length */
        len = (uint16_t)(len - MAX_TWR_RNG_DATA_NTF_OFFSET);
    }
    /*check whether vendor specific data is recieved or not*/
    if (len > 0) {
#if UWBIOT_UWBD_SR040
        uint16_t vendor_spec_length = 0;
        UWB_STREAM_TO_UINT16(vendor_spec_length, p);
        if (vendor_spec_length > 0 && vendor_spec_length == 1) {
            UWB_STREAM_TO_UINT8(pRngData->antenna_info, p);
        }
#endif //#if UWBIOT_UWBD_SR040
#if !(UWBIOT_UWBD_SR040)
        // vendor specific data length
        UWB_STREAM_TO_UINT16(pRngData->vs_length, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        /*check whether vendor specific data is recieved or not*/
        if ((pRngData->vs_length <= sizeof(VENDORSPECIFIC_MEAS)) && (pRngData->vs_length > 0)) {
            // vendor specific data type
            UWB_STREAM_TO_UINT8(pRngData->vs_data_type, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            /** NXP Specific Data (FIXED PART)*/
            UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.wifiCoExStatus, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.rxInfoMesr_twr.rxMode, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.rxInfoMesr_twr.num_of_rx_antennaRxInfo, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_ARRAY(&pRngData->vs_data.twr.rxInfoMesr_twr.rx_antennaIdRxInfo[0],
                p,
                pRngData->vs_data.twr.rxInfoMesr_twr.num_of_rx_antennaRxInfo);
            len = (uint16_t)(len - (sizeof(uint8_t)) * pRngData->vs_data.twr.rxInfoMesr_twr.num_of_rx_antennaRxInfo);
            UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.rxInfoDebugNtf_twr.rxModeDebugNtf, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.rxInfoDebugNtf_twr.num_of_rx_antennaDebugNtf, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_ARRAY(&pRngData->vs_data.twr.rxInfoDebugNtf_twr.rx_antennaIdDebugNtf[0],
                p,
                pRngData->vs_data.twr.rxInfoDebugNtf_twr.num_of_rx_antennaDebugNtf);
            len = (uint16_t)(len -
                             (sizeof(uint8_t)) * pRngData->vs_data.twr.rxInfoDebugNtf_twr.num_of_rx_antennaDebugNtf);
            /** Repitition part (This fields are repeated for each responder)*/
            for (i = 0; i < pRngData->no_of_measurements; i++) {
                for (int j = 0; j < pRngData->vs_data.twr.rxInfoMesr_twr.num_of_rx_antennaRxInfo; j++) {
                    /** AoA / PDoA measurements per RX entry*/
                    UWB_STREAM_TO_INT16(pRngData->vs_data.twr.vsMesr[i].aoaPdoaMesr_twr[j].angleOfArrival, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                    UWB_STREAM_TO_INT16(pRngData->vs_data.twr.vsMesr[i].aoaPdoaMesr_twr[j].pdoa, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                    UWB_STREAM_TO_UINT16(pRngData->vs_data.twr.vsMesr[i].aoaPdoaMesr_twr[j].pdoaIndex, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
#if UWBFTR_AoA_FoV
                    /** FoV Specific Data */
                    if (pRngData->vs_data_type == FOV_SPECIFIC_DATA_TYPE) {
                        UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.vsMesr[i].aoaPdoaMesr_twr[j].aoaFovFlag, p);
                        len = (uint16_t)(len - sizeof(uint8_t));
                    }
#endif // UWBFTR_AoA_FoV
                }
                for (int k = 0; k < pRngData->vs_data.twr.rxInfoDebugNtf_twr.num_of_rx_antennaDebugNtf; k++) {
                    /** SNRFirst / SNRMain / FirstIndex / Main Index : measurements per RX entry*/
                    UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.vsMesr[i].snrPathIndexMesr_twr[k].rxSnrFirstPath, p);
                    len = (uint16_t)(len - sizeof(uint8_t));
                    UWB_STREAM_TO_UINT8(pRngData->vs_data.twr.vsMesr[i].snrPathIndexMesr_twr[k].rxSnrMainPath, p);
                    len = (uint16_t)(len - sizeof(uint8_t));
                    UWB_STREAM_TO_INT16(pRngData->vs_data.twr.vsMesr[i].snrPathIndexMesr_twr[k].rx_FirstPathIndex, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                    UWB_STREAM_TO_INT16(pRngData->vs_data.twr.vsMesr[i].snrPathIndexMesr_twr[k].rx_MainPathIndex, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                }

                if ((pRngData->vs_data.twr.rxInfoMesr_twr.rxMode == kUWBAntCfgRxMode_ToA_Rfm_Mode) ||
                    (pRngData->vs_data.twr.rxInfoMesr_twr.rxMode == kUWBAntCfgRxMode_AoA_Rfm_Mode)) {
                    UWB_STREAM_TO_UINT16(pRngData->vs_data.twr.vsMesr[i].distance_2, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                }
            }
        }
        else {
            NXPLOG_UWBAPI_E(
                "%s: session info ntf vendor specific length exceeds the buffer limit value %d : received length "
                "%d",
                __FUNCTION__,
                sizeof(VENDORSPECIFIC_MEAS),
                pRngData->vs_length);
            return;
        }
        /** TODO: Need to handle as mandatory field in future*/
        if (len >= sizeof(uint8_t)) {
            UWB_STREAM_TO_UINT8(pRngData->authInfoPrsen, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            if (pRngData->authInfoPrsen != 0) {
                UWB_STREAM_TO_ARRAY(&pRngData->authenticationTag[0], p, AUTH_TAG_IN_16BYTES);
                len = (uint16_t)(len - AUTH_TAG_IN_16BYTES);
            }
        }
#endif // !(UWBIOT_UWBD_SR040)
    }
}
#endif // UWBFTR_TWR

#if UWBFTR_DL_TDoA_Tag
/**
 *
 * Function:        parseDlTDoARangingNtf
 *
 * Description:     Extracts Ranging Params from the given byte array for Dl TDOA ranging
 *
 * Returns:         None
 *
 */
static void parseDlTDoARangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData)
{
    uint16_t actRngRounds = 0x00; /** Number of active ranging */

    for (uint8_t i = 0; i < pRngData->no_of_measurements; i++) {
        if (pRngData->mac_addr_mode_indicator == SHORT_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_dltdoa[i].mac_addr[0], p, MAC_SHORT_ADD_LEN);
            len = (uint16_t)(len - MAC_SHORT_ADD_LEN);
        }
        else if (pRngData->mac_addr_mode_indicator == EXTENDED_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_dltdoa[i].mac_addr[0], p, MAC_EXT_ADD_LEN);
            len = (uint16_t)(len - MAC_EXT_ADD_LEN);
        }
        else {
            NXPLOG_UWBAPI_E("%s: Invalid mac addressing indicator", __FUNCTION__);
            return;
        }
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].status, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].message_type, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_dltdoa[i].message_control, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_dltdoa[i].block_index, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].round_index, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].nLoS, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_dltdoa[i].aoa_azimuth, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].aoa_azimuth_fom, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_dltdoa[i].aoa_elevation, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].aoa_elevation_fom, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_dltdoa[i].rssi, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        /** 64-bit TX timestamp */
        if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & TX_TIMESTAMP_LEN) == TX_TIMESTAMP_LEN) {
            /* 8 Octets */
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_dltdoa[i].tx_timestamp[0], p, MAX_RX_TX_TIMESTAMP);
            len = (uint16_t)(len - MAX_RX_TX_TIMESTAMP);
        }
        /** 40-bit TX timestamp */
        else if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & TX_TIMESTAMP_LEN) == 0) {
            /* 5 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_dltdoa[i].tx_timestamp[0], p, MAX_RX_TX_TIMESTAMP - 3);
            len = (uint16_t)(len - (MAX_RX_TX_TIMESTAMP - 3));
        }
        /** 64-bit RX timestamp */
        if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & RX_TIMESTAMP_LEN) == RX_TIMESTAMP_LEN) {
            /* 8 Octets */
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_dltdoa[i].rx_timestamp[0], p, MAX_RX_TX_TIMESTAMP);
            len = (uint16_t)(len - MAX_RX_TX_TIMESTAMP);
        }
        /** 40-bit RX timestamp */
        else if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & RX_TIMESTAMP_LEN) == 0) {
            /* 5 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_dltdoa[i].rx_timestamp[0], p, MAX_RX_TX_TIMESTAMP - 3);
            len = (uint16_t)(len - (MAX_RX_TX_TIMESTAMP - 3));
        }

        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_dltdoa[i].cfo_anchor, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_INT16(pRngData->ranging_meas.range_meas_dltdoa[i].cfo, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT32(pRngData->ranging_meas.range_meas_dltdoa[i].reply_time_initiator, p);
        len = (uint16_t)(len - sizeof(uint32_t));
        UWB_STREAM_TO_UINT32(pRngData->ranging_meas.range_meas_dltdoa[i].reply_time_responder, p);
        len = (uint16_t)(len - sizeof(uint32_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_dltdoa[i].initiator_responder_tof, p);
        len = (uint16_t)(len - sizeof(uint16_t));

        if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & ANCHOR_LOCATION_WGS84) ==
            ANCHOR_LOCATION_WGS84) {
            /** DT-Anchor location is included in WGS-84 coordinate system - 12 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_dltdoa[i].anchor_location[0], p, MAX_ANCHOR_LOCATIONS);
            len = (uint16_t)(len - MAX_ANCHOR_LOCATIONS);
        }
        else if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & ANCHOR_LOCATION_REL) ==
                 ANCHOR_LOCATION_REL) {
            /** DT-Anchor location is included in a relative coordinate system - 10 Octets*/
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_dltdoa[i].anchor_location[0], p, (MAX_ANCHOR_LOCATIONS - 2));
            len = (uint16_t)(len - (MAX_ANCHOR_LOCATIONS - 2));
        }
        else if ((pRngData->ranging_meas.range_meas_dltdoa[i].message_control & ANCHOR_LOCATION_WGS84) == 0) {
            /** DT-Anchor location is not included - 0 Octets */
        }
        actRngRounds = (pRngData->ranging_meas.range_meas_dltdoa[i].message_control >> ACTIVE_RR_OFSET) & MAX_ACTIVE_RR;
        UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_dltdoa[i].active_ranging_rounds[0], p, actRngRounds);
        len = (uint16_t)(len - actRngRounds);
    }
    /*check whether data is present or not*/
    /** TODO: Need to handle as mandatory field in future*/
    if (len >= sizeof(uint32_t)) {
        /** Antenna Rx Configuration information used in current ranging round*/
        UWB_STREAM_TO_UINT32(pRngData->antenna_pairInfo, p);
        len = (uint16_t)(len - sizeof(uint32_t));
    }
    if (len >= sizeof(uint8_t)) {
        /** Status code for WLAN during ranging RR*/
        UWB_STREAM_TO_UINT8(pRngData->wifiCoExStatusCode, p);
        len = (uint16_t)(len - sizeof(uint8_t));
    }
    if (len >= sizeof(uint8_t)) {
        /** Indicator for presence of Authentication Tag*/
        UWB_STREAM_TO_UINT8(pRngData->authInfoPrsen, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        if (pRngData->authInfoPrsen != 0) {
            /**Authentication Tag*/
            UWB_STREAM_TO_ARRAY(&pRngData->authenticationTag[0], p, AUTH_TAG_IN_16BYTES);
            len = (uint16_t)(len - AUTH_TAG_IN_16BYTES);
        }
    }
}
#endif // UWBFTR_DL_TDoA_Tag

#if UWBFTR_UL_TDoA_Anchor
/**
 *
 * Function:        parseOneWayRangingNtf
 *
 * Description:     Extracts Ranging Params from the given byte array for one way ranging
 *
 * Returns:         None
 *
 */
static void parseOneWayRangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData)
{
    uint8_t message_control = 0x00;
    // LOG_MAU8_I(" IN API ->",p,len);

    for (uint8_t i = 0; i < pRngData->no_of_measurements; i++) {
        if (pRngData->mac_addr_mode_indicator == SHORT_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].mac_addr[0], p, MAC_SHORT_ADD_LEN);
            len = (uint16_t)(len - MAC_SHORT_ADD_LEN);
        }
        else if (pRngData->mac_addr_mode_indicator == EXTENDED_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].mac_addr[0], p, MAC_EXT_ADD_LEN);
            len = (uint16_t)(len - MAC_EXT_ADD_LEN);
        }
        else {
            NXPLOG_UWBAPI_E("%s: Invalid MAC addressing indicator", __FUNCTION__);
            return;
        }
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].status, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].message_control, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].frame_type, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].nLos, p);
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_tdoa[i].aoa_azimuth, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].aoa_azimuth_FOM, p);
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_tdoa[i].aoa_elevation, p);
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_tdoa[i].aoa_elevation_FOM, p);
        UWB_STREAM_TO_UINT32(pRngData->ranging_meas.range_meas_tdoa[i].frame_number, p);
        len = (uint16_t)(len - ONEWAY_RFU_BYTE_OFFSET);

        message_control = pRngData->ranging_meas.range_meas_tdoa[i].message_control;
        // Set initial values as 0, if not present for Tx timestamp and device ID
        phOsalUwb_SetMemory(&pRngData->ranging_meas.range_meas_tdoa[i].tx_timestamp, 0, ULTDOA_64BIT_IN_BYTES);
        phOsalUwb_SetMemory(&pRngData->ranging_meas.range_meas_tdoa[i].ul_tdoa_device_id, 0, ULTDOA_64BIT_IN_BYTES);

        if ((message_control & ULTDOA_64BIT_RX_TIMESTAMP_MASK) == ULTDOA_64BIT_RX_TIMESTAMP_MASK) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].rx_timestamp[0], p, ULTDOA_64BIT_IN_BYTES);
            len = (uint16_t)(len - sizeof(uint64_t));
        }
        else {
            /* 5 Octets */
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].rx_timestamp[0], p, ULTDOA_40BIT_IN_BYTES);
            len = (uint16_t)(len - ULTDOA_40BIT_IN_BYTES);
        }

        if ((message_control & ULTDOA_DEVICE_ID_MASK) == ULTDOA_DEVICE_ID_16BIT_VALUE) {
            /* 2 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_tdoa[i].ul_tdoa_device_id[0], p, ULTDOA_16BIT_IN_BYTES);
            len = (uint16_t)(len - sizeof(uint16_t));
        }
        else if ((message_control & ULTDOA_DEVICE_ID_MASK) == ULTDOA_DEVICE_ID_32BIT_VALUE) {
            /* 4 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_tdoa[i].ul_tdoa_device_id[0], p, ULTDOA_32BIT_IN_BYTES);
            len = (uint16_t)(len - sizeof(uint32_t));
        }
        else if ((message_control & ULTDOA_DEVICE_ID_MASK) == ULTDOA_DEVICE_ID_64BIT_VALUE) {
            /* 8 Octets */
            UWB_STREAM_TO_ARRAY(
                &pRngData->ranging_meas.range_meas_tdoa[i].ul_tdoa_device_id[0], p, ULTDOA_64BIT_IN_BYTES);
            len = (uint16_t)(len - sizeof(uint64_t));
        }

        if ((message_control & ULTDOA_40BIT_TX_TIMESTAMP_MASK) == ULTDOA_40BIT_TX_TIMESTAMP_MASK) {
            /* 5 Octets */
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].tx_timestamp[0], p, ULTDOA_40BIT_IN_BYTES);
            len = (uint16_t)(len - ULTDOA_40BIT_IN_BYTES);
        }
        if ((message_control & ULTDOA_64BIT_TX_TIMESTAMP_MASK) == ULTDOA_64BIT_TX_TIMESTAMP_MASK) {
            /* 8 Octets */
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_tdoa[i].tx_timestamp[0], p, ULTDOA_64BIT_IN_BYTES);
            len = (uint16_t)(len - sizeof(uint64_t));
        }
    }
    if (len > 0) {
#if UWBIOT_UWBD_SR040
        uint16_t vendor_spec_length = 0;
        /*check whether vendor specific data is recieved or not*/
        UWB_STREAM_TO_UINT16(vendor_spec_length, p);

        if (vendor_spec_length > 0 && vendor_spec_length == 1) {
            UWB_STREAM_TO_UINT8(pRngData->antenna_info, p);
        }
#endif // UWBIOT_UWBD_SR040
#if !(UWBIOT_UWBD_SR040)
        // vendor specific data length
        UWB_STREAM_TO_UINT8(pRngData->vs_length, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        /*check whether vendor specific data is recieved or not*/
        if ((pRngData->vs_length <= sizeof(VENDORSPECIFIC_MEAS)) && (pRngData->vs_length > 0)) {
            /** NXP Specific Data (FIXED PART)*/
            UWB_STREAM_TO_INT16(pRngData->vs_data.tdoa.rssi_rx1, p);
            len = (uint16_t)(len - sizeof(uint16_t));
            UWB_STREAM_TO_INT16(pRngData->vs_data.tdoa.rssi_rx2, p);
            len = (uint16_t)(len - sizeof(uint16_t));
            UWB_STREAM_TO_UINT8(pRngData->vs_data.tdoa.noOfPdoaMeasures, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            /** Repitition part (This fields are repeated for each responder)*/
            for (uint8_t k = 0; k < pRngData->vs_data.tdoa.noOfPdoaMeasures; k++) {
                /** AoA / PDoA measurements per RX entry*/
                UWB_STREAM_TO_INT16(pRngData->vs_data.tdoa.pdoa[k], p);
                len = (uint16_t)(len - sizeof(uint16_t));
                UWB_STREAM_TO_UINT16(pRngData->vs_data.tdoa.pdoaIndex[k], p);
                len = (uint16_t)(len - sizeof(uint16_t));
            }
        }
        else {
            NXPLOG_UWBAPI_E(
                "%s: session info ntf vendor specific length exceeds the buffer limit value %d : received length "
                "%d",
                __FUNCTION__,
                sizeof(VENDORSPECIFIC_MEAS),
                pRngData->vs_length);
            return;
        }
        /** TODO: Need to handle as mandatory field in future*/
        if (len >= sizeof(uint32_t)) {
            /** Antenna Rx Configuration information used in current ranging round*/
            UWB_STREAM_TO_UINT32(pRngData->antenna_pairInfo, p);
            len = (uint16_t)(len - sizeof(uint32_t));
        }
        if (len >= sizeof(uint8_t)) {
            /** Status code for WLAN during ranging RR*/
            UWB_STREAM_TO_UINT8(pRngData->wifiCoExStatusCode, p);
            len = (uint16_t)(len - sizeof(uint8_t));
        }
        if (len >= sizeof(uint8_t)) {
            /** Indicator for presence of Authentication Tag*/
            UWB_STREAM_TO_UINT8(pRngData->authInfoPrsen, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            if (pRngData->authInfoPrsen != 0) {
                /**Authentication Tag*/
                UWB_STREAM_TO_ARRAY(&pRngData->authenticationTag[0], p, AUTH_TAG_IN_16BYTES);
                len = (uint16_t)(len - AUTH_TAG_IN_16BYTES);
            }
        }
#endif // !(UWBIOT_UWBD_SR040)
    }
}
#endif // UWBFTR_UL_TDoA_Anchor

#if (UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)
/**
 *
 * Function:        parseOwrWithAoaNtf
 *
 * Description:     Extracts Ranging Params from the given byte array for OWR with AoA
 *
 * Returns:         None
 *
 */
static void parseOwrWithAoaNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData)
{
    for (uint8_t i = 0; i < pRngData->no_of_measurements; i++) {
        if (pRngData->mac_addr_mode_indicator == SHORT_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_owr_aoa[i].mac_addr[0], p, MAC_SHORT_ADD_LEN);
            len = (uint16_t)(len - MAC_SHORT_ADD_LEN);
        }
        else if (pRngData->mac_addr_mode_indicator == EXTENDED_MAC_ADDRESS) {
            UWB_STREAM_TO_ARRAY(&pRngData->ranging_meas.range_meas_owr_aoa[i].mac_addr[0], p, MAC_EXT_ADD_LEN);
            len = (uint16_t)(len - MAC_EXT_ADD_LEN);
        }
        else {
            NXPLOG_UWBAPI_E("%s: Invalid mac addressing indicator", __FUNCTION__);
            return;
        }
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_owr_aoa[i].status, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_owr_aoa[i].nLos, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_owr_aoa[i].frame_seq_num, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_owr_aoa[i].block_index, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_owr_aoa[i].aoa_azimuth, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_owr_aoa[i].aoa_azimuth_FOM, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        UWB_STREAM_TO_UINT16(pRngData->ranging_meas.range_meas_owr_aoa[i].aoa_elevation, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        UWB_STREAM_TO_UINT8(pRngData->ranging_meas.range_meas_owr_aoa[i].aoa_elevation_FOM, p);
        len = (uint16_t)(len - sizeof(uint8_t));
    }
    if (len > 0) {
        /** Vendor specific data Length*/
        UWB_STREAM_TO_UINT16(pRngData->vs_length, p);
        len = (uint16_t)(len - sizeof(uint16_t));
        /*check whether vendor specific data is recieved or not*/
        if ((pRngData->vs_length <= sizeof(VENDORSPECIFIC_MEAS)) && (pRngData->vs_length > 0)) {
            /** Vendor specific data type*/
            UWB_STREAM_TO_UINT8(pRngData->vs_data_type, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            /** NXP Specific Data (FIXED PART)*/
            /** Rx Antenna Info for AoA Measurements*/
            UWB_STREAM_TO_UINT8(pRngData->vs_data.owr_aoa.rxInfoMesr_owr.rxMode, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_UINT8(pRngData->vs_data.owr_aoa.rxInfoMesr_owr.num_of_rx_antennaRxInfo, p);
            len = (uint16_t)(len - sizeof(uint8_t));
            UWB_STREAM_TO_ARRAY(&pRngData->vs_data.owr_aoa.rxInfoMesr_owr.rx_antennaIdRxInfo[0],
                p,
                pRngData->vs_data.owr_aoa.rxInfoMesr_owr.num_of_rx_antennaRxInfo);
            len =
                (uint16_t)(len - (sizeof(uint8_t) * pRngData->vs_data.owr_aoa.rxInfoMesr_owr.num_of_rx_antennaRxInfo));

            for (uint8_t i = 0; i < pRngData->no_of_measurements; i++) {
                for (int j = 0; j < pRngData->vs_data.owr_aoa.rxInfoMesr_owr.num_of_rx_antennaRxInfo; j++) {
                    /** Repitition part(This fields are repeated for each responder)*/
                    /** AoA / PDoA measurements per RX entry*/
                    /** Angle of arrival*/
                    UWB_STREAM_TO_INT16(pRngData->vs_data.owr_aoa.vsMesr[i].aoaPdoaMesr_owr[j].angleOfArrival, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                    /** Phase difference of arrival*/
                    UWB_STREAM_TO_INT16(pRngData->vs_data.owr_aoa.vsMesr[i].aoaPdoaMesr_owr[j].pdoa, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
                    /** Phase difference of arrival index in the whole CIR */
                    UWB_STREAM_TO_INT16(pRngData->vs_data.owr_aoa.vsMesr[i].aoaPdoaMesr_owr[j].pdoaIndex, p);
                    len = (uint16_t)(len - sizeof(uint16_t));
#if UWBIOT_UWBD_SR150
                    /** FoV Specific Data */
                    if (pRngData->vs_data_type == FOV_SPECIFIC_DATA_TYPE) {
                        UWB_STREAM_TO_UINT8(pRngData->vs_data.owr_aoa.vsMesr[i].aoaPdoaMesr_owr[j].aoaFovFlag, p);
                        len = (uint16_t)(len - sizeof(uint8_t));
                    }
#endif // UWBIOT_UWBD_SR150
                }
                /**RSSI*/
                UWB_STREAM_TO_INT16(pRngData->vs_data.owr_aoa.vsMesr[i].rssi, p);
                len = (uint16_t)(len - sizeof(uint16_t));
            }
        }
        else {
            NXPLOG_UWBAPI_E(
                "%s: session info ntf vendor specific length exceeds the buffer limit value %d : received length "
                "%d",
                __FUNCTION__,
                sizeof(VENDORSPECIFIC_MEAS),
                pRngData->vs_length);
            return;
        }
    }

    /*check whether data is present or not*/
    /** TODO: Need to handle as mandatory field in future*/
    if (len >= sizeof(uint8_t)) {
        UWB_STREAM_TO_UINT8(pRngData->authInfoPrsen, p);
        len = (uint16_t)(len - sizeof(uint8_t));
        if (pRngData->authInfoPrsen != 0) {
            UWB_STREAM_TO_ARRAY(&pRngData->authenticationTag[0], p, AUTH_TAG_IN_16BYTES);
            len = (uint16_t)(len - AUTH_TAG_IN_16BYTES);
        }
    }
}
#endif //(UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)

void parseRangingNtf(uint8_t *p, uint16_t len, phRangingData_t *pRngData)
{
    UWB_STREAM_TO_UINT32(pRngData->seq_ctr, p);
    UWB_STREAM_TO_UINT32(pRngData->sessionHandle, p);
    UWB_STREAM_TO_UINT8(pRngData->rcr_indication, p);
    UWB_STREAM_TO_UINT32(pRngData->curr_range_interval, p);
    UWB_STREAM_TO_UINT8(pRngData->ranging_measure_type, p);
    p++; // skip rfu byte
    UWB_STREAM_TO_UINT8(pRngData->mac_addr_mode_indicator, p);
    UWB_STREAM_TO_UINT32(pRngData->sessionHandle_of_primary_session, p);
    p = p + RESERVED_LEN; // skip reserved bytes
    UWB_STREAM_TO_UINT8(pRngData->no_of_measurements, p);
    len = (uint16_t)(len - MAC_ADDR_OFFSET);

    if ((pRngData->ranging_measure_type != MEASUREMENT_TYPE_TWOWAY) &&
        (pRngData->ranging_measure_type != MEASUREMENT_TYPE_ONEWAY) &&
        (pRngData->ranging_measure_type != MEASUREMENT_TYPE_DLTDOA) &&
        (pRngData->ranging_measure_type != MEASUREMENT_TYPE_OWR_WITH_AOA)) {
        NXPLOG_UWBAPI_E("%s: Measurement type not matched", __FUNCTION__);
    }

#if UWBFTR_TWR // support only for DSTWR
    if (pRngData->ranging_measure_type == MEASUREMENT_TYPE_TWOWAY) {
        parseTwoWayRangingNtf(p, len, pRngData);
    }
#endif // UWBFTR_TWR
#if (UWBFTR_UL_TDoA_Anchor)
    if (pRngData->ranging_measure_type == MEASUREMENT_TYPE_ONEWAY) {
        parseOneWayRangingNtf(p, len, pRngData);
    }
#endif // UWBFTR_UL_TDoA_Anchor
#if UWBFTR_DL_TDoA_Tag
    if (pRngData->ranging_measure_type == MEASUREMENT_TYPE_DLTDOA) {
        parseDlTDoARangingNtf(p, len, pRngData);
    }
#endif // UWBFTR_DL_TDoA_Tag
#if (UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)
    if (pRngData->ranging_measure_type == MEASUREMENT_TYPE_OWR_WITH_AOA) {
        parseOwrWithAoaNtf(p, len, pRngData);
    }
#endif //(UWBIOT_UWBD_SR100T || UWBIOT_UWBD_SR200T)
}

#if UWBFTR_CSA
void parseAliroRangingNtf(uint8_t *p, uint16_t len, phAliroRangingData_t *pAliroRangingData)
{
    UWB_STREAM_TO_UINT32(pAliroRangingData->sessionHandle, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->rangingStatus, p);
    UWB_STREAM_TO_UINT32(pAliroRangingData->stsIndex, p);
    UWB_STREAM_TO_UINT16(pAliroRangingData->rangingRoundIndex, p);
    UWB_STREAM_TO_UINT16(pAliroRangingData->blockIndex, p);
    UWB_STREAM_TO_UINT16(pAliroRangingData->distance, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->uncertanityAnchorFom, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->uncertanityInitiatorFom, p);
    UWB_STREAM_TO_ARRAY(pAliroRangingData->ccmTag, p, MAX_CCM_TAG_SIZE);
    UWB_STREAM_TO_INT16(pAliroRangingData->aoa_azimuth, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->aoa_azimuth_FOM, p);
    UWB_STREAM_TO_INT16(pAliroRangingData->aoa_elevation, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->aoa_elevation_FOM, p);
    /** Antenna Pair Info*/
    UWB_STREAM_TO_UINT8(pAliroRangingData->antenna_pair_info.configMode, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->antenna_pair_info.antPairId1, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->antenna_pair_info.antPairId2, p);
    UWB_STREAM_TO_UINT8(pAliroRangingData->antenna_pair_info.rfu, p);
    /** PDoA Measurements*/
    UWB_STREAM_TO_UINT8(pAliroRangingData->noOfPdoaMeasures, p);
    for (size_t i = 0; i < pAliroRangingData->noOfPdoaMeasures; i++) {
        UWB_STREAM_TO_INT16(pAliroRangingData->pdoaMeasurements[i].pdoa, p);
        UWB_STREAM_TO_UINT16(pAliroRangingData->pdoaMeasurements[i].pdoaIndex, p);
    }
    /** RSSI Measurements */
    UWB_STREAM_TO_UINT8(pAliroRangingData->noOfRssiMeasurements, p);
    if (pAliroRangingData->noOfRssiMeasurements <= MAX_NO_OF_CCC_RSSI_MEASUREMENTS) {
        for (size_t i = 0; i < pAliroRangingData->noOfRssiMeasurements; i++) {
            UWB_STREAM_TO_INT16(pAliroRangingData->cccRssiMeasurements[i].rssi_rx1, p);
            UWB_STREAM_TO_INT16(pAliroRangingData->cccRssiMeasurements[i].rssi_rx2, p);
        }
    }
    else {
        NXPLOG_UWBAPI_W("%s: Invalid Range of RSSI Measurements : %d, Expected was %d",
            __FUNCTION__,
            pAliroRangingData->noOfRssiMeasurements,
            MAX_NO_OF_CCC_RSSI_MEASUREMENTS);
    }
    /** SNR Measurements */
    UWB_STREAM_TO_UINT8(pAliroRangingData->noOfSnrMeasurements, p);
    if (pAliroRangingData->noOfSnrMeasurements <= MAX_NO_OF_CCC_SNR_MEASUREMENTS) {
        for (size_t i = 0; i < pAliroRangingData->noOfSnrMeasurements; i++) {
            UWB_STREAM_TO_UINT8(pAliroRangingData->cccSnrMeasurements[i].slotIndexAndAntennaMap, p);
            UWB_STREAM_TO_UINT8(pAliroRangingData->cccSnrMeasurements[i].snrFirstPath, p);
            UWB_STREAM_TO_UINT8(pAliroRangingData->cccSnrMeasurements[i].snrMainPath, p);
            UWB_STREAM_TO_UINT16(pAliroRangingData->cccSnrMeasurements[i].snrTotal, p);
        }
    }
    else {
        NXPLOG_UWBAPI_W("%s: Invalid Range of SNR Measurements : %d, Expected was %d",
            __FUNCTION__,
            pAliroRangingData->noOfSnrMeasurements,
            MAX_NO_OF_CCC_SNR_MEASUREMENTS);
    }
}
#endif // UWBFTR_CSA

#if UWBFTR_Radar
/**
 *
 * Function:        parseRadarCirNtf
 *
 * Description:     Extracts Radar Params from the given byte array for Radar CIR notification structure
 *
 * Returns:         None
 *
 */
static void parseRadarCirNtf(uint8_t *p, uint16_t len, phUwbRadarNtf_t *pRadarNtf)
{
    pRadarNtf->radar_ntf.radr_cir.cir_len = len - RADAR_CIR_NTF_HEADER;
    UWB_STREAM_TO_UINT16(pRadarNtf->radar_ntf.radr_cir.num_cirs, p);
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_ntf.radr_cir.cir_taps, p);
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_ntf.radr_cir.rfu, p);
    /*Application/Demo needs to allcoate the memory for CIR Data. It will not happen in the API context*/
    pRadarNtf->radar_ntf.radr_cir.cirdata = p;
}

/**
 *
 * Function:        parseRadarTestNtf
 *
 * Description:     Extracts Radar Params from the given byte array for Radar Test Isolation notification structure
 *
 * Returns:         None
 *
 */
static void parseRadarTestNtf(uint8_t *p, uint16_t len, phUwbRadarNtf_t *pRadarNtf)
{
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_ntf.radar_tst_ntf.antenna_tx, p);
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_ntf.radar_tst_ntf.antenna_rx, p);
    UWB_STREAM_TO_UINT16(pRadarNtf->radar_ntf.radar_tst_ntf.anteena_isolation, p);
}

void parseRadarNtf(uint8_t *p, uint16_t len, phUwbRadarNtf_t *pRadarNtf, uint8_t *pRadarNtfBuff)
{
    UWB_STREAM_TO_UINT32(pRadarNtf->sessionHandle, p);
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_status, p);
    UWB_STREAM_TO_UINT8(pRadarNtf->radar_type, p);
    if (pRadarNtf->radar_type == RADAR_MEASUREMENT_TYPE_CIR) {
        parseRadarCirNtf(p, len, pRadarNtf);
    }
    else if (pRadarNtf->radar_type == RADAR_MEASUREMENT_TYPE_TEST_ISOLATION) {
        parseRadarTestNtf(p, len, pRadarNtf);
    }
    else {
        NXPLOG_UWBAPI_E("%s: Measurement type not matched", __FUNCTION__);
    }

    if (NULL != pRadarNtfBuff) {
        phOsalUwb_MemCopy(pRadarNtfBuff, pRadarNtf->radar_ntf.radr_cir.cirdata, pRadarNtf->radar_ntf.radr_cir.cir_len);
    }
}
#endif // UWBFTR_Radar

#if UWBFTR_DataTransfer
void parseDataRcvNtf(uint8_t *p, uint16_t len, phUwbRcvDataPkt_t *pRcvDataPkt)
{
    UWB_STREAM_TO_UINT32(pRcvDataPkt->sessionHandle, p);
    UWB_STREAM_TO_UINT8(pRcvDataPkt->status, p);
    UWB_STREAM_TO_ARRAY(pRcvDataPkt->src_address, p, MAC_EXT_ADD_LEN);
    UWB_STREAM_TO_UINT16(pRcvDataPkt->sequence_number, p);
    UWB_STREAM_TO_UINT16(pRcvDataPkt->data_size, p);
    UWB_STREAM_TO_ARRAY(pRcvDataPkt->data, p, pRcvDataPkt->data_size);
}
#endif // UWBFTR_DataTransfer

#if !(UWBIOT_UWBD_SR040)
void deserializeDataFromRxPerNtf(phTestPer_Rx_Ntf_t *pRfTestRecvData, uint8_t *pRespBuf)
{
    UWB_STREAM_TO_UINT32(pRfTestRecvData->attempts, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->acq_Detect, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->acq_Reject, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->rxfail, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->sync_cir_ready, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->sfd_fail, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->sfd_found, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->phr_dec_error, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->phr_bit_error, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->psdu_dec_error, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->psdu_bit_error, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->sts_found, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->eof, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->vs_data_len, pRespBuf);
    if (pRfTestRecvData->vs_data_len > 0) {
#if !(UWBIOT_UWBD_SR040)
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data_type, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.rx_mode, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.no_of_rx_antenna, pRespBuf);
        UWB_STREAM_TO_ARRAY(
            &pRfTestRecvData->vs_data.rx_antenna_id[0], pRespBuf, pRfTestRecvData->vs_data.no_of_rx_antenna);
        for (int j = 0; j < pRfTestRecvData->vs_data.no_of_rx_antenna; j++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.rssi_rx[j], pRespBuf);
        }
        for (int k = 0; k < pRfTestRecvData->vs_data.no_of_rx_antenna; k++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.snr_rx[k], pRespBuf);
        }
        UWB_STREAM_TO_INT16(pRfTestRecvData->rx_cfo_est, pRespBuf);
#endif // !(UWBIOT_UWBD_SR040)
    }
}

void deserializeDataFromLoopbackNtf(phTest_Loopback_Ntf_t *pRfTestRecvData, uint8_t *pRespBuf, uint8_t *pPsdu)
{
    UWB_STREAM_TO_UINT32(pRfTestRecvData->tx_ts_int, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->tx_ts_frac, pRespBuf);
    UWB_STREAM_TO_UINT32(pRfTestRecvData->rx_ts_int, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->rx_ts_frac, pRespBuf);
    UWB_STREAM_TO_INT16(pRfTestRecvData->aoa_azimuth, pRespBuf);
    UWB_STREAM_TO_INT16(pRfTestRecvData->aoa_elevation, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->phr, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->psdu_len, pRespBuf);
    pRfTestRecvData->pPsdu = pPsdu;
    UWB_STREAM_TO_ARRAY(&pRfTestRecvData->pPsdu[0], pRespBuf, pRfTestRecvData->psdu_len);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->vs_data_len, pRespBuf);
    if (pRfTestRecvData->vs_data_len > 0) {
#if !(UWBIOT_UWBD_SR040)
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data_type, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.rx_mode, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.no_of_rx_antenna, pRespBuf);
        UWB_STREAM_TO_ARRAY(
            &pRfTestRecvData->vs_data.rx_antenna_id[0], pRespBuf, pRfTestRecvData->vs_data.no_of_rx_antenna);
        for (int j = 0; j < pRfTestRecvData->vs_data.no_of_rx_antenna; j++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.rssi_rx[j], pRespBuf);
        }
        for (int k = 0; k < pRfTestRecvData->vs_data.no_of_rx_antenna; k++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.snr_rx[k], pRespBuf);
        }
        UWB_STREAM_TO_INT16(pRfTestRecvData->rx_cfo_est, pRespBuf);
#endif // !(UWBIOT_UWBD_SR040)
    }
}

void deserializeDataFromRxNtf(phTest_Rx_Ntf_t *pRfTestRecvData, uint8_t *pRespBuf, uint8_t *pPsduBuf)
{
    UWB_STREAM_TO_UINT32(pRfTestRecvData->rx_done_ts_int, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->rx_done_ts_frac, pRespBuf);
    UWB_STREAM_TO_INT16(pRfTestRecvData->aoa_azimuth, pRespBuf);
    UWB_STREAM_TO_INT16(pRfTestRecvData->aoa_elevation, pRespBuf);
    UWB_STREAM_TO_UINT8(pRfTestRecvData->toa_gap, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->phr, pRespBuf);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->psdu_len, pRespBuf);
    pRfTestRecvData->pPsdu = pPsduBuf;
    UWB_STREAM_TO_ARRAY(&pRfTestRecvData->pPsdu[0], pRespBuf, pRfTestRecvData->psdu_len);
    UWB_STREAM_TO_UINT16(pRfTestRecvData->vs_data_len, pRespBuf);
    if (pRfTestRecvData->vs_data_len > 0) {
#if !(UWBIOT_UWBD_SR040)
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data_type, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.rx_mode, pRespBuf);
        UWB_STREAM_TO_UINT8(pRfTestRecvData->vs_data.no_of_rx_antenna, pRespBuf);
        UWB_STREAM_TO_ARRAY(
            &pRfTestRecvData->vs_data.rx_antenna_id[0], pRespBuf, pRfTestRecvData->vs_data.no_of_rx_antenna);
        for (int j = 0; j < pRfTestRecvData->vs_data.no_of_rx_antenna; j++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.rssi_rx[j], pRespBuf);
        }
        for (int k = 0; k < pRfTestRecvData->vs_data.no_of_rx_antenna; k++) {
            UWB_STREAM_TO_INT16(pRfTestRecvData->vs_data.snr_rx[k], pRespBuf);
        }
#endif // !(UWBIOT_UWBD_SR040)
    }
}
#endif // !(UWBIOT_UWBD_SR040)

#if UWBIOT_UWBD_SR040
void parseMsgLogNtf(uint8_t *p, uint16_t len, phPhyLogNtfnData_t *pTestPhyLogNtfnData)
{
    if (len != 0) {
        pTestPhyLogNtfnData->size = len;
        phOsalUwb_MemCopy(&pTestPhyLogNtfnData->data[0], p, len);
    }
}

void parseExtPsduLogNtf(uint8_t *p, uint16_t len, phPhyLogNtfnData_t *pTestPhyLogNtfnData)
{
    uint16_t psduDataLen = (p[EXT_PSDU_LEN_OFFSET] | (p[EXT_PSDU_LEN_OFFSET + 1] << 8));
    if (len != 0) {
        if (psduDataLen > MAX_HPRF_PSDU_DATA_PACKET_SIZE)
            NXPLOG_UWBAPI_W("Incomplete chained packet or wrong data received: psduData Length %ld", psduDataLen);
        else {
            NXPLOG_UWBAPI_I("PSDU Data Length: %ld", psduDataLen);
            pTestPhyLogNtfnData->size = psduDataLen;
            /** Recevied data = Session ID(4byes) + psdu Length (2bytes) + psdu data (1024 bytes)*/
            phOsalUwb_MemCopy(&pTestPhyLogNtfnData->data[0], &p[EXT_PSDU_LEN_OFFSET + 2], psduDataLen);
        }
    }
    else {
        NXPLOG_UWBAPI_E("%s: PSDU Lenth is NULL", __FUNCTION__);
    }
}

void parseTestLoopbackData(uint8_t *p, uint16_t len, phTestLoopbackData_t *pTestLoopbackStatus)
{
    pTestLoopbackStatus->status = FAILURE;
    if (len != 0) {
        UWB_STREAM_TO_UINT8(pTestLoopbackStatus->status, p);
        UWB_STREAM_TO_UINT32(pTestLoopbackStatus->groupDelay, p);
    }
}

#endif // UWBIOT_UWBD_SR040

#if UWBFTR_CSA
uint16_t serializeSessionSetLocZoneCmd(phSessionSetLocZone_t *pSetLocZone, uint8_t *pCmdBuf)
{
    uint16_t offset = 0;
    UWB_UINT32_TO_STREAM(pCmdBuf, pSetLocZone->setLocZone_SessionHandle);
    offset = (uint16_t)(offset + sizeof(pSetLocZone->setLocZone_SessionHandle));
    UWB_UINT16_TO_STREAM(pCmdBuf, pSetLocZone->setLocZone_RangingBlockIndex);
    offset = (uint16_t)(offset + sizeof(pSetLocZone->setLocZone_RangingBlockIndex));
    UWB_UINT8_TO_STREAM(pCmdBuf, pSetLocZone->setLocZone_LocZone);
    offset = (uint16_t)(offset + sizeof(pSetLocZone->setLocZone_LocZone));
    return offset;
}
#endif // UWBFTR_CSA

void PRINTF_WITH_TIME(const char *fmt, ...)
{
    /* No printing here */
}
